home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / fileread.zip / FILEREAD.TXT < prev    next >
Text File  |  1992-04-16  |  22KB  |  707 lines

  1.  
  2.               ╔═════════════════════════════════════════╗
  3.               ║                                         ║
  4.               ║    FileRead Programmer's Library 1.0    ║
  5.               ║                                         ║
  6.               ╠═════════════════════════════════════════╣
  7.               ║                                         ║
  8.               ║       Copyright 1992, Tom Clancy        ║
  9.               ║          All Rights Reserved            ║
  10.               ║                                         ║
  11.               ╚═════════════════════════════════════════╝
  12.  
  13.  
  14.  
  15. INTRODUCTION
  16.  
  17.      The FileRead programmer's library is a set of tools that allow you,
  18. the programmer, to open and manipulate a data file with any data type
  19. you choose.  It's fully object oriented and extensible so using this
  20. database is as easy as declaring an instance of the TFread object,
  21. initializing the file, then manipulating the data.
  22.  
  23. SHAREWARE NOTICE
  24.  
  25.      FileRead is shareware, not freeware, and a donation of $20.00 is
  26. asked for the registration and use of this product.  This is a
  27. programmer's tool.  I require no special registration for commercial use
  28. or for use in a software company, as long as the product is registered
  29. under that company's name and this toolkit is used for developing
  30. software, either for commercial use or in-house, specifically for that
  31. company.
  32.  
  33. REQUIREMENTS
  34.  
  35.      This toolkit requires Turbo Pascal 6.0 and will work on anything
  36. that Turbo Pascal 6.0 will work on, including my lowly tandy 1000 SX,
  37. the computer on which I designed FileRead.
  38.  
  39. FILES
  40.  
  41.      You should have the following files in your distribution disk or
  42. archive:
  43.  
  44.      Fileread.Pas........The fileread library source code
  45.      Fileread.Tpu........The fileread library Tpu version 6 format
  46.      Fileread.Txt........This documentation
  47.      Test.Pas............Test #1 source code
  48.      Test2.Pas...........Test #2 source code
  49.      L.Exe...............File listing program
  50.      Readme.Bat..........Batch file to launch file viewer
  51.      Register.Doc........Registration form to be filled out and sent in
  52.  
  53.      If you do not have the complete set of files, then either notify
  54. the sysop of the board from which you downloaded it, or, when you send
  55. in the registration, you will receive a new disk with the latest
  56. version.
  57.  
  58.      If you do not have all the files in your archive and would like to
  59. have the full version for evaluation before registering, then call me or
  60. send me a letter and we'll work out a way to get you the complete set of
  61. files.
  62.  
  63. METHOD AND OBJECT SUMMARY
  64.  
  65.      The following is the FileRead object and a description of each
  66. method plus example source code on how to use each method.
  67.  
  68. ***********************************************************************
  69.  
  70.    TFreadPtr = ^TFreadObj;  {Pointer to the TFread object}
  71.    TFreadObj = Object
  72.      Constructor Init(fn : string; mode:integer; recsize : longint);
  73.      Destructor  Done;
  74.  
  75.      { random access methods. }
  76.      Procedure   ReadRec(var frec; fpos:longint);  virtual;
  77.      Procedure   WriteRec(var frec; fpos:longint); virtual;
  78.  
  79.      { sequential access methods. }
  80.      Procedure   AppendRec(var frec);
  81.      Procedure   ReadNext(var frec);
  82.      Procedure   ReadPrevious(var frec);
  83.      Procedure   ReadCurrent(var frec);
  84.  
  85.      { various file modification methods. }
  86.      Procedure   EraseFile;
  87.      Function    RenameFile(fn:string):boolean;
  88.  
  89.      { miscellaneous functions and error flag functions. }
  90.      Procedure   Rewind;
  91.      Function    NumRecs     : Longint;
  92.      Function    GetFilename : String;
  93.      Function    GetCurrent  : Longint;
  94.      Function    OpenError   : boolean;
  95.      Function    ReadError   : boolean;
  96.      Function    WriteError  : boolean;
  97.    End;
  98.  
  99. ***********************************************************************
  100.  
  101.  
  102. Constructor Init(fn : string; mode:integer; recsize : longint);
  103.  
  104.      The constructor takes, as its parameters, the filename in a String
  105. format, the mode for which TFread will initialize the file with, and the
  106. size of the records that will be stored in and retrieved from the file.
  107.  
  108.      The file name can be a single filename such as INFO.DAT, or it can
  109. handle a path, such as C:\DATABASE\DATA\INFO.DAT.
  110.  
  111.      The mode can be one of three choice, OPEN, CREATE, or OPENCREATE.
  112.  
  113.      OPEN
  114.  
  115.           will attempt to open the file.  If the file does not exist,
  116.      then the OpenError flag will be set.
  117.  
  118.      CREATE
  119.  
  120.           will attempt to create the file whether or not it exists.
  121.      If the file does exist, then it will be rewritten, unallocating the
  122.      data that was previously in the file.  If it cannot create the file
  123.      for some reason, then the OpenError flag will be set.
  124.  
  125.      OPENCREATE
  126.  
  127.           will first attempt to open the file specified.  If the
  128.      file does not exist, TFread will then attempt to create the file.  If
  129.      the file, for some reason, cannot be created, then the OpenError flag
  130.      will be set.
  131.  
  132. Example:
  133.  
  134.      Type
  135.        InfoRec : Record
  136.          Name : String[25];
  137.          Address : String[25];
  138.          City : String[25];
  139.          State : String[2];
  140.          ZipCode : String[5];
  141.        end;
  142.  
  143.      Procedure DoSomething;
  144.      Var
  145.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  146.        Info : InforRec;
  147.      Begin
  148.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoRec)));
  149.        . . .
  150.      End;
  151.  
  152.  
  153. ----------------------------------------------------------------------------
  154.  
  155. Destructor  Done;
  156.  
  157.      The destructor will close the file, setting the OpenError flag to
  158. true so that you cannot access any of the methods.
  159.  
  160. Example:
  161.  
  162.      Type
  163.        InfoRec : Record
  164.          Name : String[25];
  165.          Address : String[25];
  166.          City : String[25];
  167.          State : String[2];
  168.          ZipCode : String[5];
  169.        end;
  170.  
  171.      Procedure DoSomething;
  172.      Var
  173.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  174.        Info : InforRec;
  175.      Begin
  176.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoRec)));
  177.        . . .
  178.        Dispose(MyFile,Done);
  179.      End;
  180.  
  181.  
  182. ----------------------------------------------------------------------------
  183.  
  184. Procedure   ReadRec(var frec; fpos:longint);
  185.  
  186.      ReadRec will read a record at the specified location, returning it
  187. in the variable frec.  If an invalid File Position (fpos) is passed to
  188. the procedure, or if for some reason ReadRec could not access the file
  189. data at the specified location, then the ReadError flag will be set.
  190.  
  191. Example:
  192.  
  193.      Type
  194.        InfoRec : Record
  195.          Name : String[25];
  196.          Address : String[25];
  197.          City : String[25];
  198.          State : String[2];
  199.          ZipCode : String[5];
  200.        end;
  201.  
  202.      Procedure DoSomething;
  203.      Var
  204.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  205.        Info : InforRec;
  206.      Begin
  207.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoRec)));
  208.  
  209.        MyFile^.ReadRec(Info,0);  {Read the first record in the data file}
  210.  
  211.        Dispose(MyFile,Done);
  212.      End;
  213.  
  214. ----------------------------------------------------------------------------
  215.  
  216. Procedure   WriteRec(var frec; fpos:longint);
  217.  
  218.      WriteRec will write a record passed in as Frec at the file position
  219. pointed to by fpos.  If the file position passed to WriteRec is invalid
  220. or if for some reason WriteRec cannot write a record at the specified
  221. location, then the WriteError flag will be set.
  222.  
  223. Example:
  224.  
  225.      Type
  226.        InfoRec : Record
  227.          Name : String[25];
  228.          Address : String[25];
  229.          City : String[25];
  230.          State : String[2];
  231.          ZipCode : String[5];
  232.        end;
  233.  
  234.      Procedure DoSomething;
  235.      Var
  236.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  237.        Info : InforRec;
  238.      Begin
  239.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoRec)));
  240.  
  241.        with info do
  242.        begin
  243.          Name:='Tom Clancy';
  244.          Address:='5861 Horseshoe Drive';
  245.          City:='Bethel Park';
  246.          State:='PA';
  247.          ZipCode:='15102';
  248.        end;
  249.  
  250.        MyFile^.WriteRec(Info,10); {Update the 10th record in the file}
  251.        MyFile^.ReadRec(Info,10);  {Read the 10th record from in data file}
  252.  
  253.        Dispose(MyFile,Done);
  254.      End;
  255.  
  256. ----------------------------------------------------------------------------
  257.  
  258. Procedure   AppendRec(var frec);
  259.  
  260.      AppendRec is a sequential access method for appending records to
  261. the end of the data file.  Like WriteRec, if AppendRec cannot append a
  262. record to the file, the WriteError flag will be set.
  263.  
  264. Example:
  265.  
  266.      Type
  267.        InfoType : Word;  {A file of unsigned integers!}
  268.  
  269.      Procedure DoSomething;
  270.      Var
  271.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  272.        Info : InfoType;
  273.        i : integer;
  274.      Begin
  275.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoType)));
  276.  
  277.        Randomize;  {randomize the random seed}
  278.        {append 100 integers to the data file.}
  279.        for i:=1 to 100 do
  280.        begin
  281.          Info:=Random(65535);
  282.          MyFile^.AppendRec(Info);
  283.        end;
  284.  
  285.        Dispose(MyFile,Done);
  286.      End;
  287.  
  288. ----------------------------------------------------------------------------
  289.  
  290. Procedure   ReadNext(var frec);
  291.  
  292.      ReadNext will read the next record in the file and return that
  293. record in the frec variable.  If ReadNext tries to read past the end of
  294. the file or if ReadNext gets an error when trying to read the next
  295. record in the file then the ReadError flag will be set.
  296.  
  297. Example:
  298.  
  299.      Type
  300.        InfoType : Word;  {A file of unsigned integers!}
  301.  
  302.      Procedure DoSomething;
  303.      Var
  304.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  305.        Info : InfoType;
  306.        i : integer;
  307.      Begin
  308.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoType)));
  309.  
  310.        MyFile^.ReadRec(Info,0);  {Start at first record!}
  311.  
  312.        {Read and display the 100 words created in the previous
  313.         example.  When ReadError is set to true, we will have reached
  314.         the end of the file.}
  315.  
  316.        while not MyFile^.ReadError do
  317.        begin
  318.          writeln(Info);
  319.          Myfile^.Readnext(Info);
  320.        end;
  321.  
  322.        Dispose(MyFile,Done);
  323.      End;
  324.  
  325.  
  326. ----------------------------------------------------------------------------
  327.  
  328. Procedure   ReadPrevious(var frec);
  329.  
  330.      ReadPrevious, like ReadNext, is a sequential file accessing method,
  331. but instead of reading forward in the file, it will read backwards
  332. through the file, returning the previous record in the frec variable.
  333. The ReadError flag will be set if ReadPrevious attempts to read a
  334. record that's one less than the first record or if for some reason an
  335. error occurs when trying to access the data file.
  336.  
  337. Example:
  338.  
  339.      Type
  340.        InfoType : Word;  {A file of unsigned integers!}
  341.  
  342.      Procedure DoSomething;
  343.      Var
  344.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  345.        Info : InfoType;
  346.        i : integer;
  347.      Begin
  348.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoType)));
  349.  
  350.        MyFile^.ReadRec(Info,MyFile^.NumRecs);  {Start at last record!}
  351.  
  352.        {Read and display backwards the 100 words randomly created. }
  353.  
  354.        while not MyFile^.ReadError do
  355.        begin
  356.          writeln(Info);
  357.          Myfile^.ReadPrevious(Info);
  358.        end;
  359.  
  360.        Dispose(MyFile,Done);
  361.      End;
  362.  
  363.  
  364. ----------------------------------------------------------------------------
  365.  
  366. Procedure   ReadCurrent(var frec);
  367.  
  368.      TFread keeps track of the current record (the last record that had
  369. been read).  Using ReadCurrent will read the record at the Current
  370. location and return it in Frec.  The same conditions apply for ther
  371. ReadError flag as they do in the previous file read methods.
  372.  
  373. Example:
  374.  
  375.      Type
  376.        InfoType : Word;  {A file of unsigned integers!}
  377.  
  378.      Procedure DoSomething;
  379.      Var
  380.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  381.        Info : InfoType;
  382.      Begin
  383.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoType)));
  384.  
  385.        . . .
  386.  
  387.        MyFile^.ReadCurrent(Info);  {Reads the current record into Info}
  388.  
  389.        Dispose(MyFile,Done);
  390.      End;
  391.  
  392. ----------------------------------------------------------------------------
  393.  
  394. Procedure   EraseFile;
  395.  
  396.      Erasefile will close the file then erase it from the disk.  Like
  397. the Done destructor, once this procedure is called, then none of the
  398. file accessing methods can be used because there will be no more file to
  399. use them with.
  400.  
  401. Example:
  402.  
  403.      Procedure DoSomething;
  404.      Var
  405.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  406.      Begin
  407.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoType)));
  408.  
  409.        . . .
  410.  
  411.        MyFile^.EraseFile;  {Erases the current file}
  412.  
  413.        Dispose(MyFile,Done);
  414.      End;
  415.  
  416. ----------------------------------------------------------------------------
  417.  
  418. Function    RenameFile(fn:string):boolean;
  419.  
  420.      RenameFile will rename the file to the specified string passed in
  421. as fn.  If the file could not be renamed (i.e. you are attempting to
  422. rename a file to another file that already exists) then the function
  423. will return false.  Renaming the file will in no way alter anything
  424. within the object.  This function will just rename the physical file on
  425. disk, leaving you where you last left off.
  426.  
  427. Example:
  428.  
  429.      Type
  430.        InfoType : Word;  {A file of unsigned integers!}
  431.  
  432.      Procedure DoSomething;
  433.      Var
  434.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  435.        Info : InfoType;
  436.        Success : Boolean;
  437.      Begin
  438.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoType)));
  439.  
  440.        . . .
  441.  
  442.        MyFile^.ReadCurrent(Info);  {Reads the current record into Info}
  443.        Success:=MyFile^.RenameFile('SOMETHING.DAT');
  444.  
  445.        {could we rename the file?}
  446.  
  447.        if Success then 
  448.          MyFile^.ReadCurrent(Info);  {Still at same position}
  449.  
  450.        Dispose(MyFile,Done);
  451.      End;
  452.  
  453. ----------------------------------------------------------------------------
  454.  
  455. Procedure   Rewind;
  456.  
  457.      Rewind will reset the file pointer and the Current position to the
  458. beginning of the file.
  459.  
  460. Example:
  461.  
  462.      Type
  463.        InfoType : Word;  {A file of unsigned integers!}
  464.  
  465.      Procedure DoSomething;
  466.      Var
  467.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  468.        Info : InfoType;
  469.        i : integer;
  470.      Begin
  471.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoType)));
  472.  
  473.        for i:=1 to 10 do
  474.        begin
  475.          MyFile^.ReadRec(Info,i-1);
  476.          Writeln(Info);
  477.        end;
  478.        {Now at the tenth record.}
  479.  
  480.        MyFile^.Rewind;   {Now at the first record!}
  481.  
  482.        . . .
  483.  
  484.        Dispose(MyFile,Done);
  485.      End;
  486.  
  487. ----------------------------------------------------------------------------
  488.  
  489. Function    NumRecs     : Longint;
  490.  
  491.      Numrecs will return the number of physical elements within the
  492. file.  For instance, if you create a file of type integer and write 100
  493. integers to disk, NumRecs will return 100.  The same applies for
  494. records, or arrays, or whatever you wish to create a file of.
  495.  
  496. Example:
  497.  
  498.      Type
  499.        InfoType : Word;  {A file of unsigned integers!}
  500.  
  501.      Procedure DoSomething;
  502.      Var
  503.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  504.        Info : InfoType;
  505.      Begin
  506.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoType)));
  507.        Writeln('Number of records in file: ',MyFile^.NumRecs);
  508.        . . .
  509.  
  510.        Dispose(MyFile,Done);
  511.      End;
  512.  
  513. ----------------------------------------------------------------------------
  514.  
  515. Function    GetFilename : String;
  516.  
  517.      GetFilename will return the actual name of the file as it appears
  518. on the disk.
  519.  
  520. Example:
  521.  
  522.      Type
  523.        InfoType : Word;  {A file of unsigned integers!}
  524.  
  525.      Procedure DoSomething;
  526.      Var
  527.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  528.        Info : InfoType;
  529.      Begin
  530.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoType)));
  531.        Writeln('The name of the file is ',MyFile^.GetFilename);
  532.        . . .
  533.  
  534.        Dispose(MyFile,Done);
  535.      End;
  536.  
  537. ----------------------------------------------------------------------------
  538.  
  539. Function    GetCurrent  : Longint;
  540.  
  541.      GetCurrent will return the physical record number where the object
  542. TFread is currently pointing.
  543.  
  544. Example:
  545.  
  546.      Type
  547.        InfoType : Word;  {A file of unsigned integers!}
  548.  
  549.      Procedure DoSomething;
  550.      Var
  551.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  552.        Info : InfoType;
  553.        i : integer;
  554.      Begin
  555.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoType)));
  556.  
  557.        {reads in the first 10 random numbers in the file and displays
  558.         the physical record number and the generated random number}
  559.        for i:=1 to 10 do
  560.        begin
  561.          MyFile^.ReadRec(Info,i-1);
  562.          Writeln(MyFile^.GetCurrent:4,'  ',Info);
  563.        end;
  564.  
  565.        Dispose(MyFile,Done);
  566.      End;
  567.  
  568. ----------------------------------------------------------------------------
  569.  
  570. Function    OpenError   : boolean;
  571.  
  572.      OpenError will return the status of the OpenError flag.  This flag
  573. is set true if the file has been successfully opened or created.
  574. Otherwise OpenError will be set to false.
  575.  
  576. Example:
  577.  
  578.      Type
  579.        InfoType : Word;  {A file of unsigned integers!}
  580.  
  581.      Procedure DoSomething;
  582.      Var
  583.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  584.        Info : InfoType;
  585.        i : integer;
  586.      Begin
  587.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoType)));
  588.        if MyFile^.OpenError then
  589.          writeln('Error openning file, aborting!')
  590.        else
  591.          . . .
  592.      End;
  593.  
  594. ----------------------------------------------------------------------------
  595.  
  596. Function    ReadError   : boolean;
  597.  
  598.      ReadError will be set true if any of the read methods have
  599. successfully read a record.  Otherwise this function will return a
  600. false.
  601.  
  602. Example:
  603.  
  604.      Type
  605.        InfoType : Word;  {A file of unsigned integers!}
  606.  
  607.      Procedure DoSomething;
  608.      Var
  609.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  610.        Info : InfoType;
  611.        i : integer;
  612.      Begin
  613.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoType)));
  614.        if MyFile^.OpenError then
  615.          writeln('Error openning file, aborting!')
  616.        else
  617.        begin
  618.          MyFile^.ReadRec(Info,101);  {attempt to read record 101!}
  619.          if MyFile^.ReadError then
  620.            writeln('Could not read that record!');
  621.          . . .
  622.        end;
  623.      End;
  624.  
  625. ----------------------------------------------------------------------------
  626.  
  627. Function    WriteError  : boolean;
  628.  
  629.      WriteError will be set to true if any of the file write methods
  630. have successfully writen the data to disk.  Otherwise this function will
  631. return false.
  632.  
  633. Example:
  634.  
  635.      Type
  636.        InfoType : Word;  {A file of unsigned integers!}
  637.  
  638.      Procedure DoSomething;
  639.      Var
  640.        MyFile : TFreadPtr;  {a pointer to the TFread object}
  641.        Info : InfoType;
  642.        i : integer;
  643.      Begin
  644.        New(MyFile,Init('TEST.DAT',OpenCreate,SizeOf(InfoType)));
  645.        if MyFile^.OpenError then
  646.          writeln('Error openning file, aborting!')
  647.        else
  648.        begin
  649.          MyFile^.WriteRec(Info,200);  {attempt to update record 200!}
  650.          if MyFile^.WriteError then
  651.            writeln('Could not update that record!');
  652.          . . .
  653.        end;
  654.      End;
  655.  
  656. ----------------------------------------------------------------------------
  657.  
  658. Function Exist(fn:string) : Boolean;
  659.  
  660.      There is also a useful function in the interface section called
  661. Exist which will return True if the file specified in the Fn string
  662. variable exists.  This is used by the object but can also be used as a
  663. stand alone procedure.
  664.  
  665. Example:
  666.  
  667.      If not Exist('TEST.DAT') then
  668.        writeln('Error: Could not locate file!')
  669.      else
  670.        . . .
  671.  
  672. ----------------------------------------------------------------------------
  673.  
  674. ENDING NOTES
  675.  
  676.      This is merely the first version of this software library, and no
  677. attempts to include internal buffering have been attempted.  I am
  678. planning with future releases to have some kind of internal buffering
  679. that might make the file accessing faster.  Since this is an object,
  680. however, you could take it upon yourself to write your own object which
  681. would inherit mine and add your own buffering handler.
  682.  
  683.      Shareware, as you already may know, is an important part of today's
  684. computing world.  I implore you, if you use shareware and don't register,
  685. please do so.  Many authors out there would greatly appreciate the
  686. donation and the incentive may be enough to encourage the author to
  687. produce an even more robust version of his or her software.
  688.  
  689.      My address and telephone number follows as does the numbers of a
  690. few bulletin boards where I can be reached:
  691.  
  692.      Thomas J. Clancy Jr.
  693.      5861 Horseshoe Drive
  694.      Bethel Park, PA 15102
  695.      Home Phone: (412) 833-7754
  696.  
  697.      BBS's:
  698.  
  699.      The Titusville Connection : (814) 827-1234
  700.      Century Products BBS      : (412) 831-5438
  701.      Technicomm BBS            : (412) 885-4347
  702.      Thiel College BBS         : (412) 589-2039
  703.      SV Educational BBS        : (412) 962-0765
  704.  
  705.  
  706.  
  707.